package com.tarena.lbs.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tarena.lbs.basic.dao.mapper.AreaMapper;
import com.tarena.lbs.basic.pojo.po.AreaPO;
import com.tarena.lbs.basic.pojo.vo.AreaVO;
import com.tarena.lbs.basic.service.AreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public List<AreaVO> queryProvinces() {
        // 构建查询条件：depth=1表示省级，isDelete=0表示未删除
        LambdaQueryWrapper<AreaPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AreaPO::getDepth, 1)
                .eq(AreaPO::getIsDelete, 0);

        // 查询数据
        List<AreaPO> areaPOList = areaMapper.selectList(queryWrapper);

        // 转换为VO对象
        return areaPOList.stream().map(areaPO -> {
            AreaVO areaVO = new AreaVO();
            BeanUtils.copyProperties(areaPO, areaVO);
            return areaVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<AreaVO> queryChildren(Long parentId) {
        // 构建查询条件：parentId匹配，isDelete=0表示未删除
        LambdaQueryWrapper<AreaPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AreaPO::getParentId, parentId)
                .eq(AreaPO::getIsDelete, 0);

        // 查询数据
        List<AreaPO> areaPOList = areaMapper.selectList(queryWrapper);

        // 转换为VO对象
        return areaPOList.stream().map(areaPO -> {
            AreaVO areaVO = new AreaVO();
            BeanUtils.copyProperties(areaPO, areaVO);
            return areaVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<AreaVO> queryTree() {
        // 1. 查询所有未删除的地区数据
        LambdaQueryWrapper<AreaPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AreaPO::getIsDelete, 0);
        List<AreaPO> allAreas = areaMapper.selectList(queryWrapper);

        // 2. 转换为VO对象
        List<AreaVO> allAreaVOs = allAreas.stream().map(areaPO -> {
            AreaVO areaVO = new AreaVO();
            BeanUtils.copyProperties(areaPO, areaVO);
            return areaVO;
        }).collect(Collectors.toList());

        // 3. 构建树形结构
        // 3.1 按父ID分组
        Map<Long, List<AreaVO>> parentMap = allAreaVOs.stream()
                .collect(Collectors.groupingBy(AreaVO::getParentId));

        // 3.2 设置子节点
        allAreaVOs.forEach(areaVO -> {
            List<AreaVO> children = parentMap.get(areaVO.getId());
            if (children != null) {
                areaVO.setChildren(children);
            } else {
                areaVO.setChildren(new ArrayList<>());
            }
        });

        // 3.3 返回顶级节点（省级）
        return allAreaVOs.stream()
                .filter(areaVO -> areaVO.getParentId() == 0)
                .collect(Collectors.toList());
    }
}